package portlet;

import java.io.IOException;

import java.util.StringTokenizer;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;


public class chitietthuctuc extends ADFBridgePortlet {
    private String thutucid;
    private String langid;
    private String urlchitiet;
    private String urldownload;
    private Object coquanid;
    private String tencoquan;
    private Object filedinhkem;
    private DCIteratorBinding thutucIterator;
    private DCIteratorBinding linhvucIterator;
    private DCIteratorBinding coquanIterator;
    private DCIteratorBinding fileIterator;
    private boolean checkcount = false;
    private boolean hasFile = false;
    private String viewRender;
    //Label
    private String labelPortlet;
    private String labelTukhoa;
    private String labelLinhvuc;
    private String labelTieude;
    private String labelTieudeDanhmuc;
    private String labelChitietThutuc;
    private String labelTrinhtu;
    private String labelCachthuc;
    private String labelThanhphan;
    private String labelThoihan;
    private String labelDoituong;
    private String labelKetqua;
    private String labelLephi;
    private String labelMautokhai;
    private String labelDieukien;
    private String labelCancuphaply;
    private String labelTencoquan;
    private String labelTaptindinhkem;
    private String labelThutuckhac;
    private String labelTinhtrangThutuc;
    private String labelConhieuluc;
    private String labelHethieuluc;

    public String getViewRender() {
        DCIteratorBinding coquanActiveIter =
            Utils.getIteratorBinding("TthcCoquanView1Iterator");
        coquanActiveIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
        coquanActiveIter.executeQuery();
        getLangid();
        getThutucid();
        if (thutucid != null) {
            getChitietThutuc();
        }
        return viewRender;
    }

    public void getChitietThutuc() {
        thutucIterator = Utils.getIteratorBinding("TthcThutucView1Iterator");
        ViewObject vo = thutucIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "=" + thutucid;
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            filedinhkem = vo.getCurrentRow().getAttribute("FileId");
            coquanid = vo.getCurrentRow().getAttribute("CoquanId");
            getCoquanThuchien();
            getHtFileObject();
            getThutucKhac();
        }
    }

    public void getCoquanThuchien() {
        DCIteratorBinding coquanActiveIter =
            Utils.getIteratorBinding("TthcCoquanView1Iterator");
        ViewObject vo = coquanActiveIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "IN (0)";
        if (coquanid != null) {
            varStr = "IN (" + coquanid.toString() + ")";
        }
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();

        if (vo.getAllRowsInRange().length > 0) {
            tencoquan = "";
            for (int i = 0; i < vo.getAllRowsInRange().length; i++) {
                tencoquan +=
                        vo.getRowAtRangeIndex(i).getAttribute("Tieude").toString() +
                        ", ";
            }
            tencoquan = tencoquan.substring(0, tencoquan.lastIndexOf(","));
        }
    }

    public void getHtFileObject() {
        fileIterator = Utils.getIteratorBinding("HtFileView1Iterator");
        ViewObject vo = fileIterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        ViewCriteriaRow cr = vc.createViewCriteriaRow();
        String varStr = "IN (0)";
        if (getFiledinhkem() != null) {
            hasFile = true;
            varStr = "IN (" + getFiledinhkem() + ")";
        }
        cr.setAttribute("Id", varStr);
        vc.addElement(cr);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();

    }

    public void getThutucKhac() {
        if (thutucid != null) {
            DCIteratorBinding thutuckhacIterator =
                Utils.getIteratorBinding("TthcThutuckhacView1Iterator");
            ViewObject vo = thutuckhacIterator.getViewObject();
            ViewCriteria vc =
                vo.getViewCriteriaManager().getViewCriteria("TthcThutuckhacViewCriteria");
            vo.ensureVariableManager().setVariableValue("varId", thutucid);
            vo.ensureVariableManager().setVariableValue("varLang", langid);
            vo.getViewCriteriaManager().removeViewCriteria("TthcThutuckhacViewCriteria");
            vo.getViewCriteriaManager().applyViewCriteria(vc);
            vo.setRangeSize(10);
            vo.executeQuery();
            if (vo.getAllRowsInRange().length > 0) {
                checkcount = true;
            }
        } else {
            checkcount = false;
        }
        Utils.iteratorRefresh("TthcThutuckhacView1Iterator");
    }

    public String getUrlchitiet() {
        try {
            urlchitiet = Utils.getPortletRequest().getParameter("urlchitiet");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlchitiet;
    }

    public String getUrldownload() {
        try {
            urldownload =
                    Utils.getPortletRequest().getParameter("urldownload");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldownload;
    }

    public String getLangid() {
        langid = Utils.getPortalLocale().getLanguage();
        return langid;
    }

    public String getThutucid() {
        try {
            thutucid = Utils.getPortletRequest().getParameter("id");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return thutucid;
    }

    public String getTencoquan() {
        return tencoquan;
    }

    public Object getCoquanid() {
        return coquanid;
    }

    public Object getFiledinhkem() {
        return filedinhkem;
    }

    public DCIteratorBinding getThutucIterator() {
        return thutucIterator;
    }

    public DCIteratorBinding getLinhvucIterator() {
        return linhvucIterator;
    }

    public DCIteratorBinding getCoquanIterator() {
        return coquanIterator;
    }

    public DCIteratorBinding getFileIterator() {
        return fileIterator;
    }

    public boolean isCheckcount() {
        return checkcount;
    }

    public boolean isHasFile() {
        return hasFile;
    }

    public String getLabelPortlet() {
        try {
            labelPortlet =
                    Utils.getPortletRequest().getParameter("tieudeportlet");
            if (labelPortlet == null) {
                labelPortlet = Utils.getResource("THU_TUC_HANH_CHINH");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return labelPortlet;
    }

    public String getLabelTukhoa() {
        labelTukhoa = Utils.getResource("TU_KHOA");
        return labelTukhoa;
    }

    public String getLabelLinhvuc() {
        labelLinhvuc = Utils.getResource("LINH_VUC_THUC_HIEN");
        return labelLinhvuc;
    }

    public String getLabelTieude() {
        labelTieude = Utils.getResource("TIEU_DE");
        return labelTieude;
    }

    public String getLabelTieudeDanhmuc() {
        labelTieudeDanhmuc = Utils.getResource("TIEU_DE_DANH_MUC");
        return labelTieudeDanhmuc;
    }

    public String getLabelChitietThutuc() {
        labelChitietThutuc = Utils.getResource("TIEU_DE_CHI_TIET_THU_TUC");
        return labelChitietThutuc;
    }

    public String getLabelTrinhtu() {
        labelTrinhtu = Utils.getResource("TRINH_TU_THUC_HIEN");
        return labelTrinhtu;
    }

    public String getLabelCachthuc() {
        labelCachthuc = Utils.getResource("CACH_THUC_HIEN");
        return labelCachthuc;
    }

    public String getLabelThanhphan() {
        labelThanhphan = Utils.getResource("THANH_PHAN");
        return labelThanhphan;
    }

    public String getLabelThoihan() {
        labelThoihan = Utils.getResource("THOI_HAN");
        return labelThoihan;
    }

    public String getLabelDoituong() {
        labelDoituong = Utils.getResource("DOI_TUONG");
        return labelDoituong;
    }

    public String getLabelKetqua() {
        labelKetqua = Utils.getResource("KET_QUA");
        return labelKetqua;
    }

    public String getLabelLephi() {
        labelLephi = Utils.getResource("LE_PHI");
        return labelLephi;
    }

    public String getLabelMautokhai() {
        labelMautokhai = Utils.getResource("MAU_TO_KHAI");
        return labelMautokhai;
    }

    public String getLabelDieukien() {
        labelDieukien = Utils.getResource("DIEU_KIEN");
        return labelDieukien;
    }

    public String getLabelCancuphaply() {
        labelCancuphaply = Utils.getResource("CAN_CU_PHAP_LY");
        return labelCancuphaply;
    }

    public String getLabelTencoquan() {
        labelTencoquan = Utils.getResource("TEN_CO_QUAN");
        return labelTencoquan;
    }

    public String getLabelTaptindinhkem() {
        labelTaptindinhkem = Utils.getResource("TAP_TIN_DINH_KEM");
        return labelTaptindinhkem;
    }

    public String getLabelThutuckhac() {
        labelThutuckhac = Utils.getResource("THU_TUC_KHAC");
        return labelThutuckhac;
    }

    public String getLabelTinhtrangThutuc() {
        labelTinhtrangThutuc = Utils.getResource("TINH_TRANG_THU_TUC");
        return labelTinhtrangThutuc;
    }

    public String getLabelConhieuluc() {
        labelConhieuluc = Utils.getResource("CON_HIEU_LUC");
        return labelConhieuluc;
    }

    public String getLabelHethieuluc() {
        labelHethieuluc = Utils.getResource("HET_HIEU_LUC");
        return labelHethieuluc;
    }
}
